package graphicalInterface;

/**
 * Clasa ce se ocupa de desenarea locuintei, a aparatelor si obiectelor statice din casa si 
 * de miscarea locuitorilor
 */

import java.util.ArrayList;
import main.CreateHome;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
import timer.TimerEvent;


public class HomeInterface {

	public static Canvas canvas;
	public final static Display display = new Display();
	public static Shell shell;
	
	public static final int IMAGE_WIDTH = 20;
	public static int TIMER_INTERVAL = 100;

	public static int xOmulet1 = 850;
	public static int yOmulet1 = 650;
	public static ArrayList<Integer> xDestOmulet1=new ArrayList<Integer>();
	public static ArrayList<Integer> yDestOmulet1=new ArrayList<Integer>();

	public static int xOmulet2=900;
	public static int yOmulet2=650;
	public static ArrayList<Integer> xDestOmulet2=new ArrayList<Integer>();
	public static ArrayList<Integer> yDestOmulet2=new ArrayList<Integer>();

	public static int xOmulet3=850;
	public static int yOmulet3=400;
	public static ArrayList<Integer> xDestOmulet3=new ArrayList<Integer>();
	public static ArrayList<Integer> yDestOmulet3=new ArrayList<Integer>();

	public static int xOmulet4=850;
	public static int yOmulet4=150;
	public static ArrayList<Integer> xDestOmulet4=new ArrayList<Integer>();
	public static ArrayList<Integer> yDestOmulet4=new ArrayList<Integer>();

	public static int directionX = 1;
	public static int directionY = 1;

	public static int iInterfata=0;
	public static int iInterfata2=0;
	public static int iInterfata3=0;
	public static int iInterfata4=0;

	static Font font = new Font(display,"Arial", 14, SWT.BOLD | SWT.ITALIC); 

	public HomeInterface(Shell shellP)
	{
		HomeInterface.shell = new Shell(shellP);
		HomeInterface.shell.setLayout(new FillLayout());
		HomeInterface.shell.setSize(1200, 810);
		HomeInterface.shell.open();
	}

	/**
	 * metoda de desenare al interfetei casei
	 */
	public void run() {
		createContents(HomeInterface.shell);
		HomeInterface.shell.open();

		// Set up the timer for the animation
		Runnable runnable = new Runnable() {
			public void run() {
				animate();
				HomeInterface.display.timerExec(TIMER_INTERVAL, this);
			}
		};

		// Launch the timer
		HomeInterface.display.timerExec(TIMER_INTERVAL, runnable);

		while (!HomeInterface.shell.isDisposed()) {
			if (!HomeInterface.display.readAndDispatch()) {
				HomeInterface.display.sleep();
			}
		}

		// Kill the timer
		HomeInterface.display.timerExec(-1, runnable);
		HomeInterface.display.dispose();
	}

	/**
	 * deseaneaza locuitorii
	 */
	public void drawPeople(PaintEvent e)
	{
		e.gc.setBackground(HomeInterface.shell.getDisplay().getSystemColor(SWT.COLOR_RED));
		e.gc.fillOval(xOmulet1, yOmulet1, IMAGE_WIDTH, IMAGE_WIDTH);

		e.gc.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_MAGENTA));
		e.gc.fillOval(xOmulet2, yOmulet2, IMAGE_WIDTH, IMAGE_WIDTH);

		e.gc.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_DARK_GREEN));
		e.gc.fillOval(xOmulet3, yOmulet3, IMAGE_WIDTH, IMAGE_WIDTH);

		e.gc.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_CYAN));
		e.gc.fillOval(xOmulet4, yOmulet4, IMAGE_WIDTH, IMAGE_WIDTH);
	}
	/**
	 * deseneaza casa si obiectele statice
	 */
	public void drawHomeAndStaticalObjects(PaintEvent e)
	{
		e.gc.drawLine(0, 800, 1000, 800);
		e.gc.drawLine(1000, 800, 1000, 0);
		e.gc.drawLine(0, 0, 0, 800);
		//usa h-iesire
		e.gc.drawLine(0, 0, 430, 0);
		e.gc.drawLine(470, 0, 1000, 0);

		//usa l-h
		e.gc.drawLine(0, 500, 338, 500);
		e.gc.drawLine(378, 500, 480, 500);
		//usa b1-h
		e.gc.drawLine(520, 500, 780, 500);
		//usa d1-d2
		e.gc.drawLine(820, 500, 1000, 500);

		//usa l-b1
		e.gc.drawLine(400, 800, 400, 670);
		e.gc.drawLine(400, 500, 400, 630);
		//usa b1-d1
		e.gc.drawLine(600, 800, 600, 670);
		e.gc.drawLine(600, 500, 600, 630);

		e.gc.drawLine(0, 300, 300, 300);
		//usa b2
		e.gc.drawLine(300, 500, 300, 420);
		e.gc.drawLine(300, 300, 300, 380);
		//usa buc
		e.gc.drawLine(300, 300, 300, 170);
		e.gc.drawLine(300, 0, 300, 130);

		e.gc.drawLine(1000, 300, 600, 300);
		//usa d2
		e.gc.drawLine(600, 500, 600, 420);
		e.gc.drawLine(600, 300, 600, 380);
		//usa d3
		e.gc.drawLine(600, 300, 600, 170);
		e.gc.drawLine(600, 0, 600, 130);

		e.gc.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_INFO_BACKGROUND));
		e.gc.setFont(font); 
		e.gc.drawText("Living",50,700);
		e.gc.drawText("Baie 1",530,700); 
		e.gc.drawText("Dormitor 1",870,700);
		e.gc.drawText("Baie 2", 50, 470);
		e.gc.drawText("Bucatarie", 50, 270);
		e.gc.drawText("Hol", 450, 250);
		e.gc.drawText("Dormitor 2", 870, 470);
		e.gc.drawText("Dormitor 3", 870, 270);

		Image i = CreateHome.casa.canap.getImage();
		e.gc.drawImage(i, CreateHome.casa.canap.getX(), CreateHome.casa.canap.getY());

		i = CreateHome.casa.masaScaune.getImage();
		e.gc.drawImage(i, CreateHome.casa.masaScaune.getX(), CreateHome.casa.masaScaune.getY());

		i = CreateHome.casa.patD.getImage();
		e.gc.drawImage(i, CreateHome.casa.patD.getX(), CreateHome.casa.patD.getY());

		i = CreateHome.casa.patS1.getImage();
		e.gc.drawImage(i, CreateHome.casa.patS1.getX(), CreateHome.casa.patS1.getY());

		i = CreateHome.casa.patS2.getImage();
		e.gc.drawImage(i, CreateHome.casa.patS2.getX(), CreateHome.casa.patS2.getY());

		i = CreateHome.casa.plant.getImage();
		e.gc.drawImage(i, CreateHome.casa.plant.getX(), CreateHome.casa.plant.getY());
	}
	/**
	 * deseneaza aparatele
	 */
	public void drawAppliances(PaintEvent e)
	{
		Image i = CreateHome.casa.becBuc.getImage();
		e.gc.drawImage(i, CreateHome.casa.becBuc.getX(), CreateHome.casa.becBuc.getY());

		i = CreateHome.casa.becB2.getImage();
		e.gc.drawImage(i, CreateHome.casa.becB2.getX(), CreateHome.casa.becB2.getY());

		i = CreateHome.casa.becB1.getImage();
		e.gc.drawImage(i, CreateHome.casa.becB1.getX(), CreateHome.casa.becB1.getY());

		i = CreateHome.casa.becL.getImage();
		e.gc.drawImage(i, CreateHome.casa.becL.getX(), CreateHome.casa.becL.getY());

		i = CreateHome.casa.BecH.getImage();
		e.gc.drawImage(i, CreateHome.casa.BecH.getX(), CreateHome.casa.BecH.getY());

		i = CreateHome.casa.BecD1.getImage();
		e.gc.drawImage(i, CreateHome.casa.BecD1.getX(), CreateHome.casa.BecD1.getY());

		i = CreateHome.casa.BecD2.getImage();
		e.gc.drawImage(i, CreateHome.casa.BecD2.getX(), CreateHome.casa.BecD2.getY());

		i = CreateHome.casa.BecD3.getImage();
		e.gc.drawImage(i, CreateHome.casa.BecD3.getX(), CreateHome.casa.BecD3.getY());

		i = CreateHome.casa.frigi.getImage();
		e.gc.drawImage(i, CreateHome.casa.frigi.getX(), CreateHome.casa.frigi.getY());

		i = CreateHome.casa.aragaz.getImage();
		e.gc.drawImage(i, CreateHome.casa.aragaz.getX(), CreateHome.casa.aragaz.getY());

		i = CreateHome.casa.robinetBuc.getImage();
		e.gc.drawImage(i, CreateHome.casa.robinetBuc.getX(), CreateHome.casa.robinetBuc.getY());

		i = CreateHome.casa.masina.getImage();
		e.gc.drawImage(i, CreateHome.casa.masina.getX(), CreateHome.casa.masina.getY());

		i = CreateHome.casa.robinetB2.getImage();
		e.gc.drawImage(i, CreateHome.casa.robinetB2.getX(), CreateHome.casa.robinetB2.getY());

		i = CreateHome.casa.robinetBaie1.getImage();
		e.gc.drawImage(i, CreateHome.casa.robinetBaie1.getX(), CreateHome.casa.robinetBaie1.getY());

		i = CreateHome.casa.cadaBaie1.getImage();
		e.gc.drawImage(i, CreateHome.casa.cadaBaie1.getX(), CreateHome.casa.cadaBaie1.getY());

		i = CreateHome.casa.tv.getImage();
		e.gc.drawImage(i, CreateHome.casa.tv.getX(), CreateHome.casa.tv.getY());

		i = CreateHome.casa.calcL.getImage();
		e.gc.drawImage(i, CreateHome.casa.calcL.getX(), CreateHome.casa.calcL.getY());

		i = CreateHome.casa.aer.getImage();
		e.gc.drawImage(i, CreateHome.casa.aer.getX(), CreateHome.casa.aer.getY());

		i = CreateHome.casa.wc1.getImage();
		e.gc.drawImage(i, CreateHome.casa.wc1.getX(), CreateHome.casa.wc1.getY());

		i = CreateHome.casa.wc2.getImage();
		e.gc.drawImage(i, CreateHome.casa.wc2.getX(), CreateHome.casa.wc2.getY());

		i = CreateHome.casa.robinetVana.getImage();
		e.gc.drawImage(i, CreateHome.casa.robinetVana.getX(), CreateHome.casa.robinetVana.getY());
	}
	/**
	 * Creates the house window's contents
	 */
	private void createContents(final Shell shell) {
		HomeInterface.shell.addPaintListener(new PaintListener() {
			public void paintControl(PaintEvent e) {
				// Draw the background

				drawPeople(e);
				drawHomeAndStaticalObjects(e);

				String oraCurenta="";
				if (TimerEvent.now.getHours()<10)
					oraCurenta+="0"+TimerEvent.now.getHours();
				else
					oraCurenta+=TimerEvent.now.getHours();
				oraCurenta+=":";
				if (TimerEvent.now.getMinutes()<10)
					oraCurenta+="0"+TimerEvent.now.getMinutes();
				else
					oraCurenta+=TimerEvent.now.getMinutes();
				oraCurenta+=":";
				if (TimerEvent.now.getSeconds()<10)
					oraCurenta+="0"+TimerEvent.now.getSeconds();
				else
					oraCurenta+=TimerEvent.now.getSeconds();

				e.gc.drawText("ORA CURENTA:", 1020, 50);
				e.gc.drawText(oraCurenta, 1020, 100);

				drawAppliances(e);
			}
		});
	}

	/**
	 * Animates the next frame
	 */
	public void animate() {
		//primul locuitor
		if (xDestOmulet1.size()>0)
		{
			HomeInterface.TIMER_INTERVAL=1000;
			if (xOmulet1>xDestOmulet1.get(iInterfata))
			{
				directionX=-1;
				xOmulet1 += 50*directionX;
				HomeInterface.TIMER_INTERVAL=10;
			}
			else
				if (yOmulet1>yDestOmulet1.get(iInterfata) )
				{
					directionY=-1;
					yOmulet1 += 50*directionY;
					HomeInterface.TIMER_INTERVAL=10;
				}
				else
					if (xOmulet1<xDestOmulet1.get(iInterfata))
					{

						directionX=1;
						xOmulet1 += 50*directionX;
						HomeInterface.TIMER_INTERVAL=10;
					}
					else
						if (yOmulet1<yDestOmulet1.get(iInterfata) )
						{

							directionY=1;
							yOmulet1 += 50*directionY;
							HomeInterface.TIMER_INTERVAL=10;
						}
						else
						{
							HomeInterface.xDestOmulet1.remove(iInterfata);
							HomeInterface.yDestOmulet1.remove(iInterfata);
							HomeInterface.TIMER_INTERVAL=1000;
						}
		}
		//al doilea locuitor
		if (xDestOmulet2.size()>0)
		{
			HomeInterface.TIMER_INTERVAL=1000;
			if (xOmulet2>xDestOmulet2.get(iInterfata2))
			{
				directionX=-1;
				xOmulet2 += 50*directionX;
				HomeInterface.TIMER_INTERVAL=10;
			}
			else
				if (yOmulet2>yDestOmulet2.get(iInterfata2) )
				{
					directionY=-1;
					yOmulet2 += 50*directionY;
					HomeInterface.TIMER_INTERVAL=10;
				}
				else
					if (xOmulet2<xDestOmulet2.get(iInterfata2))
					{

						directionX=1;
						xOmulet2 += 50*directionX;
						HomeInterface.TIMER_INTERVAL=10;
					}
					else
						if (yOmulet2<yDestOmulet2.get(iInterfata2) )
						{

							directionY=1;
							yOmulet2 += 50*directionY;
							HomeInterface.TIMER_INTERVAL=10;
						}
						else
						{
							HomeInterface.xDestOmulet2.remove(iInterfata2);
							HomeInterface.yDestOmulet2.remove(iInterfata2);
						}
		}
		//al treilea locuitor
		if (xDestOmulet3.size()>0)
		{
			HomeInterface.TIMER_INTERVAL=1000;
			if (xOmulet3>xDestOmulet3.get(iInterfata3))
			{
				directionX=-1;
				xOmulet3 += 50*directionX;
				HomeInterface.TIMER_INTERVAL=10;
			}
			else
				if (yOmulet3>yDestOmulet3.get(iInterfata3) )
				{
					directionY=-1;
					yOmulet3 += 50*directionY;
					HomeInterface.TIMER_INTERVAL=10;
				}
				else
					if (xOmulet3<xDestOmulet3.get(iInterfata3))
					{

						directionX=1;
						xOmulet3 += 50*directionX;
						HomeInterface.TIMER_INTERVAL=10;
					}
					else
						if (yOmulet3<yDestOmulet3.get(iInterfata3) )
						{

							directionY=1;
							yOmulet3 += 50*directionY;
							HomeInterface.TIMER_INTERVAL=10;
						}
						else
						{
							HomeInterface.xDestOmulet3.remove(iInterfata3);
							HomeInterface.yDestOmulet3.remove(iInterfata3);
							HomeInterface.TIMER_INTERVAL=1000;
						}
		}
		//al patrulea locuitor
		if (xDestOmulet4.size()>0)
		{
			HomeInterface.TIMER_INTERVAL=1000;
			if (xOmulet4>xDestOmulet4.get(iInterfata4))
			{
				directionX=-1;
				xOmulet4 += 50*directionX;
				HomeInterface.TIMER_INTERVAL=10;
			}
			else
				if (yOmulet4>yDestOmulet4.get(iInterfata4) )
				{
					directionY=-1;
					yOmulet4 += 50*directionY;
					HomeInterface.TIMER_INTERVAL=10;
				}
				else
					if (xOmulet4<xDestOmulet4.get(iInterfata4))
					{

						directionX=1;
						xOmulet4 += 50*directionX;
						HomeInterface.TIMER_INTERVAL=10;
					}
					else
						if (yOmulet4<yDestOmulet4.get(iInterfata4) )
						{

							directionY=1;
							yOmulet4 += 50*directionY;
							HomeInterface.TIMER_INTERVAL=10;
						}
						else
						{
							HomeInterface.xDestOmulet4.remove(iInterfata4);
							HomeInterface.yDestOmulet4.remove(iInterfata4);
							HomeInterface.TIMER_INTERVAL=1000;
						}
		}
		// Force a redraw
		HomeInterface.shell.redraw();
	}

	/**
	 * opreste interfata si programul
	 */
	public void stopInterface()
	{
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		display.dispose();
		System.exit(1);
	}
}
